//Created by  for Jaws.Core.Web
//Original work Copyright(C) 2008.  All Rights Reserved.

using System;

namespace Jaws.Core.Web.Entities.Validation.CustomAttributes
{
    public class IsEmailAddress : ValidationAttribute
    {
        public IsEmailAddress()
        {
            Message = ValidationMessages.IsEmail;
        }

        #region Overrides of ValidationAttribute

        public override bool IsValid(object item)
        {
            if (!String.IsNullOrEmpty(item.ToString().Trim()))
            {
                var regex = new System.Text.RegularExpressions.Regex("^([0-9a-zA-Z]([-.\\w]*[0-9a-zA-Z])*@([0-9a-zA-Z][-\\w]*[0-9a-zA-Z]\\.)+[com|org|net]{2,3})$");

                if (!regex.Match(item.ToString()).Success)
                    return false;
            }
            else
                return false;

            return true;
        }

        #endregion
    }
}